document.addEventListener('DOMContentLoaded', function () {
    // 获取用户信息
    const token = localStorage.getItem('token');
    if (!token) {
        alert('请先登录');
        window.location.href = 'login.html';
        return;
    }

    fetch('/user', {
        method: 'GET',
        headers: {
            'x-access-token': token
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.user) {
            document.getElementById('user-avatar').src = data.user.avatar || 'default-avatar.png';
            document.getElementById('user-name').textContent = data.user.name || '未设置';
            document.getElementById('user-experience').textContent = data.user.experience || 0;
        }
    })
    .catch(error => {
        console.error('获取用户信息失败:', error);
    });

    // 签到按钮事件
    document.getElementById('sign-in-button').addEventListener('click', () => {
        fetch('/sign-in', {
            method: 'POST',
            headers: {
                'x-access-token': token
            }
        })
        .then(response => response.json())
        .then(data => {
            alert(data.message);
            if (data.experience) {
                document.getElementById('user-experience').textContent = data.experience;
            }
        })
        .catch(error => {
            console.error('签到失败:', error);
        });
    });

    // 设置表单提交事件
    document.getElementById('settings-form').addEventListener('submit', function (event) {
        event.preventDefault();

        const formData = new FormData();
        formData.append('name', document.getElementById('username').value);
        const avatarFile = document.getElementById('avatar').files[0];
        if (avatarFile) {
            formData.append('avatar', avatarFile);
        }

        fetch('/update-user', {
            method: 'POST',
            headers: {
                'x-access-token': token
            },
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            alert(data.message);
            if (data.message === '用户信息更新成功') {
                window.location.reload(); // 更新成功后刷新页面
            }
        })
        .catch(error => {
            console.error('更新用户信息失败:', error);
        });
    });

    // 上传视频表单提交事件
    document.getElementById('upload-form').addEventListener('submit', function(event) {
        event.preventDefault();
    
        const uploadFormData = new FormData();
        uploadFormData.append('videoTitle', document.getElementById('videoTitle').value);
        uploadFormData.append('videoCategory', document.getElementById('videoCategory').value);
        uploadFormData.append('video', document.getElementById('video').files[0]);
        uploadFormData.append('thumbnail', document.getElementById('thumbnail').files[0]);
    
        fetch('/upload', {
            method: 'POST',
            headers: {
                'x-access-token': token
            },
            body: uploadFormData
        })
        .then(response => response.json())
        .then(data => {
            alert(data.message);
            if (data.message === '上传成功') {
                loadUserVideos(); // 刷新视频列表
            }
        })
        .catch(error => {
            console.error('上传视频失败:', error);
        });
    });
    
    // 获取并显示视频详情
    function loadVideoDetails(videoId) {
        fetch(`/video/${videoId}`, {
            method: 'GET'
        })
        .then(response => response.json())
        .then(video => {
            document.getElementById('video-detail').innerHTML = `
                <h2>${video.title}</h2>
                <p>发布人: ${video.uploader}</p>
                <p>发布时间: ${new Date(video.uploadDate).toLocaleString()}</p>
                <p>播放量: ${video.views}</p>
                <p>分类: ${video.category}</p>
                <video src="${video.videoPath}" controls></video>
            `;
        })
        .catch(error => {
            console.error('获取视频详情失败:', error);
        });
    }
    
});
